﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Common;

namespace WebUI
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string username = tbxUserName.Text.Trim();
            string pwd = Md5.getMd5(tbxPassword.Text.Trim());
            switch (rbl_role.SelectedValue)
            {
                case "1":
                    if(BLL.UsersManage.UserLogin(username,pwd,false))
                    {
                        FormsAuthentication.RedirectFromLoginPage(username,true);
                        HttpCookie cookie = new HttpCookie("role", "student");
                        cookie.Expires = DateTime.Now.AddDays(10);
                        Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        //Alert.Show("登陆失败,用户名或密码错误！");
                        Response.Write("<script>window.alert('登陆失败,用户名或密码错误！')</script>");
                    }
                    break;
                case "2":
                    if (BLL.UsersManage.UserLogin(username, pwd, true))
                    {
                        FormsAuthentication.RedirectFromLoginPage(username, true);
                        HttpCookie cookie = new HttpCookie("role", "teacher");
                        cookie.Expires = DateTime.Now.AddDays(10);
                        Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        //Alert.Show("登陆失败,用户名或密码错误！");
                        Response.Write("<script>window.alert('登陆失败,用户名或密码错误！')</script>");
                    }
                    break;
                case "3":
                    if (BLL.AdminUserManage.UserLogin(username, pwd))
                    {
                        FormsAuthentication.RedirectFromLoginPage(username, true);
                        HttpCookie cookie = new HttpCookie("role","admin");
                        cookie.Expires = DateTime.Now.AddDays(10);
                        Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        //Alert.Show("登陆失败,用户名或密码错误！");
                        Response.Write("<script>window.alert('登陆失败,用户名或密码错误！')</script>");
                    }
                    break;
            }
        }
    }
}